package leetcode.剑指offer.剑指II;

import leetcode.util.CommonUtils;
import leetcode.剑指offer.pojo.ListNode;
import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/8/18 20:30
 * @description：https://leetcode-cn.com/problems/UHnkqh/
 */
public class 反转链表 {
    @Test
    public void test() {
        Solution solution = new Solution();
        ListNode head = solution.reverseList(new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5))))));
        CommonUtils.list_listNode(head);
    }


    class Solution {
        public ListNode reverseList(ListNode head) {
            ListNode curHead = new ListNode(-1);
            ListNode temp = head;
            while (temp != null) {
                ListNode next = temp.next;
                temp.next = curHead.next;
                curHead.next = temp;
                temp = next;
            }
            return curHead.next;
        }
    }
}
